// const { default: axios } = require("axios");

// const { post } = require("../../../router/home/userComment");

// const { table } = require("console");

// 上传评价
function uploadFile(){
    let commentImgUl = document.getElementsByClassName('commentImgUl')[0]
    let fileUpload = document.getElementsByClassName('fileUpload')[0];
    let reallyComment = document.getElementsByClassName('reallyComment')[0]
    let postSubmit = document.getElementsByClassName('postSubmit')[0]
    // 图片回显
    fileUpload.onchange = function(){
        commentImgUl.innerHTML = ''
        console.log(fileUpload.files);
        for(let i = 0; i<fileUpload.files.length; i++){
            let imgFile = fileUpload.files[i];
            let imgSrc = window.URL.createObjectURL(imgFile)
            
            commentImgUl.innerHTML += `
                    <li class="imgUpLi">
                        <div class="picLi">
                            <img src="${imgSrc}" alt="">
                        </div>
                        <div class="picShadow" onclick="clickNone(this)">
                            <i class="iconfont closel" style="font-size: 26px; color: white;"></i> 
                        </div>
                    </li>
                `
        }
        // 上传评价
    postSubmit.addEventListener('click', () => {
        let deleteGoodsInner = document.getElementsByClassName('deleteGoodsInner')[0]
        let formData = new FormData()
        for(var i = 0; i<fileUpload.files.length; i++){
            formData.append(`fileUpload${i}`, fileUpload.files[i])
            console.log(fileUpload.files[i]);
        }
        // 其他参数
        formData.append('userId', 15)
        formData.append('shopId', 34)
        formData.append('goodsId', 23)
        formData.append('GComment', reallyComment.value)
            axios({
                // url:`/home/userComment/commentGoods?userId=${15}&shopId=${34}&goodsId=${23}&GComment=${reallyComment.value}`,
                url:'/home/userComment/commentGoods',
                method:'POST',
                data:formData,
                headers:{
                    'Content-Type':"multipart/form-data"
                }
            })
            .then(data => {
                Pop()
                deleteGoodsInner.innerHTML = '评价成功'
                // alert('上传成功')
                console.log(data);
                commentImgUl.innerHTML = ''
                reallyComment.value = ''
                commentRea()
                
            })
            .catch(err => {
                console.log(err);
            })
    })
    }
    

    
     
}
uploadFile()
//弹窗
function Pop(){
    let deleteIcon = document.getElementsByClassName('deleteIcon')[0]
    let noDelete = document.getElementsByClassName('noDelete')[0]
    let sureDelete = document.getElementsByClassName('sureDelete')[0]
    let actionDel = document.getElementsByClassName('actionDel')[0]
    let popBox = document.getElementsByClassName('popBox')[0]
    let deleteGood = document.getElementsByClassName('deleteGood')[0]
    
    // let 
    // actionDel.onclick = function(){
        popBox.style.opacity = '1'
        popBox.style.visibility = 'visible'
        deleteGood.innerHTML = '提示'
        // deleteGoodsInner.innerHTML = '请选择完整的商品信息'
    // }
    deleteIcon.onclick = function(){
        popBox.style.opacity = '0'
        popBox.style.visibility = 'hidden'
    }
    sureDelete.onclick = function(){
        popBox.style.opacity = '0'
        popBox.style.visibility = 'hidden'
    }
    noDelete.onclick = function(){
        popBox.style.opacity = '0'
        popBox.style.visibility = 'hidden'
    }
}
function clickNone(th){
    th.parentNode.remove()
}


var strHref = document.location.toString();
console.log(strHref);//打印出URL
var intPos = strHref.indexOf("?")+1;
// var strRight = strHref.substr(intPos + 1); //获取到右边的参数部分
strHref = strHref.substring(intPos)
var arr = strHref.split('&')
var obj = {}
for(let i =0; i<arr.length; i++){
    let str1 = arr[i].split('=')[0]
    let str2 = arr[i].split('=')[1]
    obj[str1] = str2
}
console.log(obj);
// 遍历评论
function commentRea(){
    let commentGrid = document.getElementsByClassName('commentGrid')[0]
    let commentBox = commentGrid.querySelector('table')
    axios({
        url:'/home/productDetail/getAllCommentsByShopId',
        method:'GET',
        params:{
            shopId: obj.shopId
        }
    })
    .then(data => {
        console.log(data);
        commentBox.innerHTML = ``
        for(let i = 0 ; i <data.data.data.length; i++){
            commentBox.innerHTML += `
                    <tr>
                    <td class="commentMaster">
                        <div class="tdContent">
                            <div class="tdChacter">
                                ${data.data.data[i].gcomment}
                            </div>
                            <ul class="tdImg">
                                
                            </ul>
                        </div>
                        <div class="tdDate">${data.data.data[i].time}</div>
                    </td>
                    <td class="commentMeta">
                        <p><span>口味:</span>蓝莓味【送】芒果味 共发500g*2箱</p>
                    </td>
                    <td class="commentAuthor">
                        <div class="author">
                            玩
                            <span>***</span>
                            为
                        </div>
                    </td>
                </tr>
            `

            for(let j = 0; j<data.data.data[i].path.length; j++){
                let LiImg = ''
                LiImg +=`
                <li><img src="${data.data.data[i].path[j]}" alt=""></li>
                `

                if(j === data.data.data[i].path.length-1){
                    LiImg += `
                    <div style="clear: both;"></div>
                    `
                }   
                document.getElementsByClassName('tdImg')[i].innerHTML += LiImg
            }
        }
        
    })
    .catch(err => {
        console.log(err);
    })
}
commentRea()